package com.moon.back.controller;


import cn.dev33.satoken.util.SaResult;
import com.moon.back.annotation.Debounce;
import com.moon.back.entity.Req.FileQueryReq;
import com.moon.back.entity.Req.FileSingleQueryReq;
import com.moon.back.entity.Req.UploadFileReq;
import com.moon.back.entity.dto.*;
import com.moon.back.service.IFileSearchService;
import com.moon.back.service.IFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;


/**
 * 文件上传
 */


@Api( "文件管理")
@CrossOrigin
@RestController
@RequestMapping("/file")
public class FileController  {

    private static final Logger log = LoggerFactory.getLogger(FileController.class);

    @Autowired
    private IFileService fileService;

    @Autowired
    private IFileSearchService fileSearchService;



    // @ModelAttribute 注解
    // @RequestParam 注解
    //
    @ApiOperation(  "文件上传服务")
    @RequestMapping("/upload")
    public SaResult uploadFile( @RequestParam MultipartFile file, @RequestParam String path) {
        return  fileService.fileUpload(UploadFileReq.builder().file(file).path(path).build());
    }

    /**
     * 查询文件信息
     * @param request
     * @param fileQueryReq
     * @return
     *  这个是get请求
     */
    @ApiOperation( "文件查询")
    @PostMapping("/getFile")
    public SaResult queryFile(HttpServletRequest request, @RequestBody FileQueryReq fileQueryReq){
        return fileService.getFile(request, fileQueryReq);
    }

    /**
     * 删除文件
     * @param request
     * @param delFileDto
     * @return
     */
    @ApiOperation( "删除文件操作")
    @PostMapping("/delFile")
    public SaResult deleteFile(HttpServletRequest request , @RequestBody DelFileDto delFileDto){
       return fileService.delFile(request, delFileDto );
    }

    /**
     * 批量删除操作
     * @param delFileBatchDto
     * @return
     */
    @ApiOperation(  "批量删除文件操作")
    @PostMapping("/delBatchFile")
    public SaResult deleteBatchFile( @RequestBody DelFileBatchDto delFileBatchDto ){
        for (int i = 0; i < delFileBatchDto.getFileIds().length; i++) {
            log.info("Item : {}",delFileBatchDto.getFileIds()[i]);
        }

        return fileService.delFileBatch(delFileBatchDto);
    }

    /**
     * 新建文件夹
     * @param request
     * @param folderDto
     * @return
     */
    @ApiOperation(  "新建文件夹操作")
    // 加上防抖的处理
    @Debounce(waiting = 1000L,unit = TimeUnit.MILLISECONDS)
    @PostMapping("/newDir")
    public SaResult newDir (HttpServletRequest request ,@RequestBody FolderDto folderDto){
         return fileService.newDir(request,folderDto);
    }

    /**
     *
     *
     * @param request
     * @param fileChangeNameDto
     * @return
     */
    @ApiOperation(  "文件重命名操作")
    @PostMapping("/changeFileName")
    public SaResult changeFileName (HttpServletRequest request ,@RequestBody FileChangeNameDto fileChangeNameDto){
        return fileService.changeFileName(request,fileChangeNameDto);
    }

    /**
     *
     * @param request
     * @param fileId
     * @return
     */
    @ApiOperation(  "文件下载操作")
    @GetMapping("/downloadFile")
    public ResponseEntity<InputStreamResource> downFile(HttpServletRequest request ,@RequestParam("fileId") String fileId) throws IOException {
         return  fileService.downFile(request, fileId);
    }

    /**
     *
     * @param folderPath
     * @return
     * @throws IOException
     */

    /**
     * @param request
     * @param resp
     * @param path
     * @return
     * @throws IOException
     */
    @ApiOperation( "图片预览")
    @PostMapping("/lookImage")
    public SaResult lookImage (HttpServletRequest request, HttpServletResponse resp,@RequestParam("path") String path) throws IOException {
        return fileService.lookImage(path,request,resp);
    }

    /**
     * 获取到对应的type
     * @param request
     * @param fileTypeDto
     * @return
     */
    @ApiOperation( "通过类型获取文件")
    @PostMapping("/getAllFileType")
    public SaResult getAllFileType(HttpServletRequest request ,@RequestBody FileTypeDto fileTypeDto){
        return fileService.getAllFileType(request,fileTypeDto);
    }

    /**
     * @param request
     * @param resp
     * @param path
     * @return
     * @throws IOException
     */
    @ApiOperation( "查看预览文档")
    @PostMapping("/lookDoc")
    public SaResult lookDoc(HttpServletRequest request, HttpServletResponse resp,@RequestParam("path") String path) throws IOException {
         return fileService.lookDoc(request,resp,path);
    }


    @ApiOperation( "文件恢复操作")
    @PostMapping("/refresh")
    public SaResult refresh(@RequestBody RefreshDto refreshDto) throws IOException {
        return fileService.refresh(refreshDto);
    }

    @ApiOperation(" -- 文件搜索")
    @PostMapping("/search")
    public SaResult filePath(@RequestBody FileQueryReq fileQueryReq){
        return fileSearchService.FileSearch(fileQueryReq);
    }


    @ApiOperation("  文件搜索-- 获取文件详情")
    @PostMapping("/singleFile")
    public SaResult detailFile(@RequestBody FileSingleQueryReq fileSingleQueryReq){
        return fileSearchService.searchSingleById(fileSingleQueryReq);
    }

}
